##Study_3_Lucid.R
##R version 3.6.1 

##set working directory
setwd("...")  ##SET WORKING DIRECTORY HERE

data.raw<- read.csv("Lucid_survey_2_5_19_for_analysis.csv", header=TRUE)

##remove those who did not consent
data<- subset(data.raw, data.raw$Q48==1)
  
##########################################################
##              Data setup                              ##
##########################################################
##Recode key variables
##Party
data$party<- ifelse(data$Q13==1 & data$Q15==1, 1, 
                    ifelse(data$Q13==1 & data$Q15==2, 2,
                           ifelse((data$Q13==3| data$Q13==4) & data$Q19==1, 3,
                                  ifelse(data$Q13==3 & data$Q19==3, 4,
                                         ifelse((data$Q13==3 | data$Q13==4) & data$Q19==2, 5,
                                                ifelse(data$Q13==2 & data$Q17==2, 6,
                                                       ifelse(data$Q13==2 & data$Q17==1, 7, NA)))))))
summary(as.factor(data$party)) ##37 NA

data$dem<- ifelse(data$party <=3, 1, 0)
data$rep<- ifelse(data$party >=5, 1, 0)
data$ind<- ifelse(data$party==4, 1, 0)

##party before branching
data$party3.leanind<- ifelse(data$party<=2, "dem",
                             ifelse(data$party==3 | data$party==4 | data$party==5, "ind",
                                    ifelse(data$party >=6, "rep", NA)))
summary(as.factor(data$party3.leanind))

##strong partisans
data$strongpart<- ifelse(data$party==1 | data$party==7, 1, 0)
summary(as.factor(data$strongpart))

##political interest
data$pol.interest<- (data$Q23-5)/(-4)
summary(data$pol.interest)

##political trust (higher=more trust)
data$pol.trust.1<- (data$Q61-7)/(-6) ##agree=more trust
summary(data$pol.trust.1)
data$pol.trust.2<- (data$Q63-7)/(-6) ##agree=more trust
summary(data$pol.trust.2)
data$pol.trust.3<- (data$Q65.1-1)/6 ##disagree=most trust
summary(data$pol.trust.3)
data$pol.trust.4<- (data$Q67-1)/6 ##disagree=more trust
summary(data$pol.trust.4)
##Index
data$pol.trust.index<- ((data$pol.trust.1 + data$pol.trust.2 + data$pol.trust.3 + data$pol.trust.4)/4) ##Need better way to get mean if didn't answer all!
summary(data$pol.trust.index)

##DVs
##fairness of House agenda setting
data$dv.house<- (data$Q75-1)/6 ##higher values=more fair
summary(data$dv.house)
##fairness of Senate filibuster
data$dv.senate<- (data$Q78-1)/6 ##higher values=more fair
summary(data$dv.senate)
##support for No labels rule
data$dv.house.rule<- (data$Q82-7)/(-6) ##higher values=oppose so reorder
summary(data$dv.house.rule)

##Political knowledge 
##agreed to not use information
data$knowledge.agree<- (data$Q36-2)/(-1)
summary(data$knowledge.agree)
##correct answers
data$knowledge.1<- ifelse(data$Q37==4, 1, 0)
summary(data$knowledge.1)
data$knowledge.2<- ifelse(data$Q38==3, 1, 0)
summary(data$knowledge.1)
data$knowledge.3<- ifelse(data$Q39==4, 1, 0)
summary(data$knowledge.3)
data$knowledge.4<- ifelse(data$Q41==1, 1, 0)
summary(data$knowledge.4)
data$knowledge.5<- ifelse(data$Q42==3, 1, 0)
summary(data$knowledge.5)
##Number correct
data$knowledge.count<- (data$knowledge.1 + data$knowledge.2 + data$knowledge.3 + data$knowledge.4 + data$knowledge.5)
summary(data$knowledge.count) ##median is 3
##define high knowledge as 3 or more correct
data$highknow<- ifelse(data$knowledge.count >=3, 1, 0)
summary(as.factor(data$highknow))

##fraction correct
data$knowledge.prop<- data$knowledge.count/5
summary(data$knowledge.prop)

##Treatments
##House majority
summary(data$Process1Maj)
data$opp.maj<- ifelse((data$dem==1 & data$Process1Maj=="Republicans") | (data$rep==1 & data$Process1Maj=="Democrats"), 1, 0) ##includes ind in 0
data$own.maj<- ifelse((data$dem==1 & data$Process1Maj=="Democrats") | (data$rep==1 & data$Process1Maj=="Republicans"), 1, 0)
##House alternative treatment
summary(data$Process1Treatment)
data$Process1Treatment.na<- ifelse(data$Process1Treatment=="", NA,
                                   ifelse(data$Process1Treatment=="Control", "Control", 
                                        ifelse(data$Process1Treatment=="Bipartisan", "Bipartisan",
                                               ifelse(data$Process1Treatment=="Majority", "Majority",
                                                      ifelse(data$Process1Treatment=="Minority", "Minority", NA))))) ##change missing to NA
summary(as.factor(data$Process1Treatment.na)) 
data$Process1Treatment.na<- as.factor(data$Process1Treatment.na)
data$Process1Treatment.reorder<- relevel(data$Process1Treatment.na, ref="Control")
summary(data$Process1Treatment.reorder)

##do you align with Senate majority (Republicans)?
#just use dem and ind

##House rule treatment
summary(data$Process3Treatment)
data$Process3Treatment.na<- ifelse(data$Process3Treatment=="", NA,
                                   ifelse(data$Process3Treatment=="Bipartisan", "Bipartisan", 
                                          ifelse(data$Process3Treatment=="Bipartisan and Democrats", "Bipartisan and Democrats", NA))) ##change missing to NA
summary(as.factor(data$Process3Treatment.na)) 
data$Process3Treatment.na<- as.factor(data$Process3Treatment.na)
data$Process3Treatment.reorder<- relevel(data$Process3Treatment.na, ref="Bipartisan")
summary(data$Process3Treatment.reorder)

###############################################
##        Sample demographics                ##
###############################################
##Demographics for appendix
##gender (1=male, 2=female)
prop.table(table(data$Q7))
##age (categories 1-7, combine 6 and 7)
prop.table(table(data$Q9))
0.13574219+0.02636719
##region (1	Northeast, 2	Midwest, 3	South, 4	West)
prop.table(table(data$region))
##race/ethnicity (1=white, 2=black, 3=hispanic, 4=Asian, 5=other)
prop.table(table(data$Q11))
##party (before branching)
prop.table(table(data$party3.leanind))

###############################################
##              Analysis                     ##
###############################################

##Fairness of House agenda-setting
#All respondents, No interaction (do not include party indicators: opp.maj or ind) - Table 7
summary(data$dv.house) ##mean is 0.43 (so lower fairness than midpoint neither fair nor unfair)

reg1.house<- lm(dv.house ~ Process1Treatment.reorder, data=data)
summary(reg1.house) 

#add interaction (drop pure ind)
reg2.house<- lm(dv.house ~ opp.maj*Process1Treatment.reorder, data=data,
                subset=c(data$ind==0))
summary(reg2.house) 

#add controls for knowledge and trust in government (not reported in text)
reg3.house<- lm(dv.house ~ opp.maj*Process1Treatment.reorder + knowledge.prop + pol.trust.index, data=data,
                subset=c(data$ind==0))
summary(reg3.house) 

##substnative effects
##model 1
sd(data$dv.house, na.rm=TRUE)
##bipartisan
-0.17/0.2992791 ##-0.568
##minority
-0.206/0.2992791 ##-0.688

##############
##Fairness of Senate filibuster (for comparison)
summary(data$dv.senate) ##mean 0.488 (so lower fairness than midpoint neither fair nor unfair)
##t-test
t.test(data$dv.house, data$dv.senate)

##############
##Support for No Labels Rule - Appendix Table E11
summary(data$dv.house.rule) ##mean 0.6429 (above midpoint of neither support nor oppose)

##All respondents, no interactions
reg1.house.rule<- lm(dv.house.rule ~ rep + ind + Process3Treatment.reorder, data=data)
summary(reg1.house.rule)

#add interaction (based on actual party control) and subset to partisans
reg2.house.rule<- lm(dv.house.rule ~ rep*Process3Treatment.reorder, data=data,
                     subset=c(data$ind==0))
summary(reg2.house.rule)

#add controls for knowledge and trust
reg3.house.rule<- lm(dv.house.rule ~ rep*Process3Treatment.reorder + knowledge.prop + pol.trust.index, data=data,
                     subset=c(data$ind==0))
summary(reg3.house.rule)


##################################################
##Robustness checks of House results and House rules (just models 1 and 2)

##strong partisans - Appendix Table E12
##House
reg1.strongpart.house<- lm(dv.house ~ Process1Treatment.reorder, data=data,
                            subset=c(data$strongpart==1))
summary(reg1.strongpart.house) 

#add interaction (drop pure ind)
reg2.strongpart.house<- lm(dv.house ~ opp.maj*Process1Treatment.reorder, data=data,
                subset=c(data$ind==0 & data$strongpart==1))
summary(reg2.strongpart.house) 

##House rule
reg1.strongpart.house.rule<- lm(dv.house.rule ~ rep + Process3Treatment.reorder, data=data,
                                subset=c(data$strongpart==1))
summary(reg1.strongpart.house.rule)

#add interaction (based on actual party control) and subset to partisans
reg2.strongpart.house.rule<- lm(dv.house.rule ~ rep*Process3Treatment.reorder, data=data,
                     subset=c(data$ind==0 & data$strongpart==1))
summary(reg2.strongpart.house.rule)

######################################
##High knowledge (3 or more correct) - Appendix Table E13
##House
reg1.highknow.house<- lm(dv.house ~ Process1Treatment.reorder, data=data,
                           subset=c(data$highknow==1))
summary(reg1.highknow.house) 

#add interaction (drop pure ind)
reg2.highknow.house<- lm(dv.house ~ opp.maj*Process1Treatment.reorder, data=data,
                           subset=c(data$ind==0 & data$highknow==1))
summary(reg2.highknow.house) 

##House rule
reg1.highknow.house.rule<- lm(dv.house.rule ~ ind + rep + Process3Treatment.reorder, data=data,
                                subset=c(data$highknow==1))
summary(reg1.highknow.house.rule)

#add interaction (based on actual party control) and subset to partisans
reg2.highknow.house.rule<- lm(dv.house.rule ~ rep*Process3Treatment.reorder, data=data,
                                subset=c(data$ind==0 & data$highknow==1))
summary(reg2.highknow.house.rule)

#############################
##Independents and Leaners - Appendix Table E14
##House
reg1.indlean.house<- lm(dv.house ~ Process1Treatment.reorder, data=data,
                         subset=c(data$party3.leanind=="ind"))
summary(reg1.indlean.house) 

##House rule
reg1.indlean.house.rule<- lm(dv.house.rule ~ Process3Treatment.reorder, data=data,
                              subset=c(data$party3.leanind=="ind"))
summary(reg1.indlean.house.rule)


##################################################
##Output regressions
source("regtable.R")

##set working directory where files are located
setwd(".../output") ##SET WORKING DIRECTORY HERE

##Table 7
outtable.rtf(list("(1) House Fairness"=reg1.house, "(2) House Fairness"=reg2.house),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("Process1Treatment.reorderBipartisan", "Ignore Bipartisan"),
                              c("Process1Treatment.reorderMajority", "Pursue Majority"),
                              c("Process1Treatment.reorderMinority", "Ignore Minority"),
                              c("opp.maj", "Minority Voters"),
                              c("opp.maj:Process1Treatment.reorderBipartisan", "Ignore Bipartisan x Min. Voters"),
                              c("opp.maj:Process1Treatment.reorderMajority", "Pursue Majority x Min. Voters"),
                              c("opp.maj:Process1Treatment.reorderMinority", "Ignore Minority x Min. Voters")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Table 7.rtf")

##Appendix Table E11
outtable.rtf(list("(1) No Labels Proposal"=reg1.house.rule, "(2) No Labels Proposal"=reg2.house.rule, "(3) No Labels Proposal"=reg3.house.rule),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("Process1Treatment.reorderBipartisan", "Ignore Bipartisan"),
                              c("Process1Treatment.reorderMajority", "Pursue Majority"),
                              c("Process1Treatment.reorderMinority", "Ignore Minority"),
                              c("ind", "Independents"),
                              c("knowledge.prop", "Prop. Knowledge Correct"),
                              c("pol.trust.index", "Trust in Government"),
                              c("dem", "Opposing Majority"),
                              c("Process3Treatment.reorderBipartisan and Democrats", "Dem Support Proposal"),
                              c("rep", "Republicans"),
                              c("rep:Process3Treatment.reorderBipartisan and Democrats", "Dem Support Proposal x Rep")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Appendix Table E11.rtf")

##Appendix robustness - strong partisans Table E12
##Just House and House rule - models 1 and 2
outtable.rtf(list("(1) House Fairness"=reg1.strongpart.house, "(2) House Fairness"=reg2.strongpart.house,
                  "(3) No Labels Proposal"=reg1.strongpart.house.rule, "(4) No Labels Proposal"=reg2.strongpart.house.rule),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("Process1Treatment.reorderBipartisan", "Ignore Bipartisan"),
                              c("Process1Treatment.reorderMajority", "Pursue Majority"),
                              c("Process1Treatment.reorderMinority", "Ignore Minority"),
                              c("opp.maj", "Minority Voters"),
                              c("ind", "Independents"),
                              c("opp.maj:Process1Treatment.reorderBipartisan", "Ignore Bipartisan x Min. Voters"),
                              c("opp.maj:Process1Treatment.reorderMajority", "Pursue Majority x Min. Voters"),
                              c("opp.maj:Process1Treatment.reorderMinority", "Ignore Minority x Min. Voters"),
                              c("knowledge.prop", "Prop. Knowledge Correct"),
                              c("pol.trust.index", "Trust in Government"),
                              c("dem", "Opposing Majority"),
                              c("Process3Treatment.reorderBipartisan and Democrats", "Dem Support Proposal"),
                              c("rep", "Republicans"),
                              c("rep:Process3Treatment.reorderBipartisan and Democrats", "Dem Support Proposal x Rep")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Appendix Table E12.rtf")

##Appendix robustness - high knowldge Table E13
##Just House and House rule - models 1 and 2
outtable.rtf(list("(1) House Fairness"=reg1.highknow.house, "(2) House Fairness"=reg2.highknow.house,
                  "(3) No Labels Proposal"=reg1.highknow.house.rule, "(4) No Labels Proposal"=reg2.highknow.house.rule),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("Process1Treatment.reorderBipartisan", "Ignore Bipartisan"),
                              c("Process1Treatment.reorderMajority", "Pursue Majority"),
                              c("Process1Treatment.reorderMinority", "Ignore Minority"),
                              c("opp.maj", "Minority Voters"),
                              c("ind", "Independents"),
                              c("opp.maj:Process1Treatment.reorderBipartisan", "Ignore Bipartisan x Min. Voters"),
                              c("opp.maj:Process1Treatment.reorderMajority", "Pursue Majority x Min. Voters"),
                              c("opp.maj:Process1Treatment.reorderMinority", "Ignore Minority x Min. Voters"),
                              c("knowledge.prop", "Prop. Knowledge Correct"),
                              c("pol.trust.index", "Trust in Government"),
                              c("dem", "Opposing Majority"),
                              c("Process3Treatment.reorderBipartisan and Democrats", "Dem Support Proposal"),
                              c("rep", "Republicans"),
                              c("rep:Process3Treatment.reorderBipartisan and Democrats", "Dem Support Proposal x Rep")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Appendix Table E13.rtf")

##Appendix - Independents (pure and leaners) Table E14
outtable.rtf(list("(1) House Fairness"=reg1.indlean.house, "(2)No Labels Proposal"=reg1.indlean.house.rule),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("Process1Treatment.reorderBipartisan", "Ignore Bipartisan"),
                              c("Process1Treatment.reorderMajority", "Pursue Majority"),
                              c("Process1Treatment.reorderMinority", "Ignore Minority"),
                              c("Process3Treatment.reorderBipartisan and Democrats", "Dem Support Proposal")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Appendix Table E14.rtf")


